const db = require('../config/db')

// 定义轮播图类
class Banner {
    // 添加轮播图
    add(banner_title, banner_link, banner_picUrl, banner_create_time) {
        return new Promise((resolve, reject) => {
            db.query(`select * from db_banner where banner_title = '${banner_title}'`)
                .then(
                    rows => {
                        if (rows.length == 0) {
                            // 添加轮播图
                            let sql = `insert into db_banner(banner_title, banner_link,banner_picUrl,banner_create_time) 
                        values('${banner_title}','${banner_link}','${banner_picUrl}','${banner_create_time}')`;
                            db.query(sql)
                                .then(
                                    content => {
                                        resolve({
                                            code: 200,
                                            msg: "发布轮播图成功。",
                                            content
                                        })
                                    },
                                    err => {
                                        reject({
                                            code: 500,
                                            msg: "发布轮播图失败。",
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            // 不添加
                            resolve({
                                code: 1,
                                msg: "该标题对应的轮播图已存在。"
                            })
                        }
                    },
                    err => {
                        reject({
                            code: 500,
                            msg: "服务器出点小问题。",
                            err
                        })
                    }
                )
        })
    }

    // 删除轮播图
    remove(id) {
        return new Promise((resolve, reject) => {
            db.query(`select * from db_banner where id = '${id}'`)
                .then(
                    rows => {
                        if (rows && rows.length === 1) {
                            // 存在
                            let sql = `delete from db_banner where id = '${id}'`
                            db.query(sql)
                                .then(
                                    content => {
                                        resolve({
                                            code: 200,
                                            msg: "删除轮播图记录成功",
                                            content
                                        })
                                    },
                                    err => {
                                        reject({
                                            code: 500,
                                            msg: "删除轮播图记录失败",
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            // 不存在
                            resolve({
                                code: 404,
                                msg: "不存在这个轮播图"
                            })
                        }
                    },
                    err => {
                        reject({
                            code: 500,
                            msg: "查询轮播图数据失败",
                            err
                        })
                    }
                )
        })
    }

    // 获取轮播图列表（分页）
    async list(page, pageSize) {
        // 获取用户总数
        let total;
        await this.total().then(
            content => {
                total = content.result[0].total
            }
        )

        return new Promise((resolve, reject) => {
            //获取分页数据
            let page_num = ((parseInt(page) - 1) * parseInt(pageSize))
            let page_size = (parseInt(pageSize))
            let sql = `select * from db_banner limit ${page_num} , ${page_size}`
            db.query(sql)
                .then(
                    result => {
                        resolve({
                            code: 200,
                            total,
                            msg: "获取轮播图列表成功。",
                            result
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "获取轮播图列表失败",
                            err
                        })
                    }
                )

        })
    }

    // 统计轮播图记录总数量
    total(){
        return new Promise((resolve,reject)=>{
            let sql = 'select count(*) as total from db_banner';
            db.query(sql)
            .then(
                result => {
                    resolve({
                        code: 200,
                        msg: "统计数据成功。",
                        result
                    })
                },
                err => {
                    reject({
                        code: -1,
                        msg: "统计信息失败",
                        err
                    })
                }
            )
        })
    }
}
module.exports = new Banner();